package com.klun.designMode.proxy.JDKProxy1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理工厂
 */
public class MyBeanFactory<T> {

    public static UserService createUserservice() {
        //目标类
        final UserService userService = new UserServiceImpl();
        //生成代理类
        UserService proxyService = (UserService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 添加在 每个接口方法的 method -invoke之前
                System.out.println("执行前");
                Object obj = method.invoke(userService, args);
                System.out.println("执行后");
                System.out.println("========");
                return obj;
            }
        });
        return proxyService;
    }
}